package com.sinochem.yunlian.ship.common.util;

import com.google.common.base.Strings;

import java.text.DecimalFormat;

/**
 * @author: chengyiping
 * @email: chengyiping@sinochem.com
 * @description:
 * @date: Created in 2018/5/15 18:17
 * @modified By :
 */
public class GPSFormatUtils {

    /**
     * 功能： 度-->度分秒
     * @param data 传入待转化格式的经度或者纬度
     */
    public static String DDtoDMS(String data){

        String[] array=data.split("[.]");
        String degrees=array[0];//得到度

        Double m=Double.parseDouble("0."+array[1])*60;  //分

        return degrees + "°" + String.format("%.1f",m) + "'";
    }

    /**
     * 经度转换
     * @param data
     * @return
     */
    public static String longitudeTS(String data){
        if(Strings.isNullOrEmpty(data)){
            return "";
        }
        String suffix = "E";
        if(Double.parseDouble(data) < 0.0){
            suffix = "W";
        }
        return DDtoDMS(String.valueOf(Math.abs(Double.parseDouble(data)))) + " " + suffix;
    }

    /**
     * 纬度转换
     * @param data
     * @return
     */
    public static String latitudeTS(String data){
        if(Strings.isNullOrEmpty(data)){
            return "";
        }
        String suffix = "N";
        if(Double.parseDouble(data) < 0.0){
            suffix = "S";
        }
        return DDtoDMS(String.valueOf(Math.abs(Double.parseDouble(data)))) + " " + suffix;
    }

    /**
     *
     * @param data
     * @return
     */
    public static String round(String data){
        if(Strings.isNullOrEmpty(data)){
            return "";
        }
        return String.format("%.1f",Double.parseDouble(data));
    }

    public static  void main(String args[]){
        System.out.println(GPSFormatUtils.latitudeTS("-30.5531221212121"));

        String s = String.format("%.1f",3.1565926);
        System.out.println(s);

    }
}
